<template>
  <div v-if="visible" class="popup-panel-mask" @click.self="handleMaskClick">
    <div class="popup-panel-content" :style="panelStyle">
      <div class="popup-panel-header">
        <span class="popup-panel-title">{{ title }}</span>
        <span class="popup-panel-close" @click="close">×</span>
      </div>
      <div class="popup-panel-body">
        <slot />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch, onMounted } from 'vue'

const props = defineProps({
  modelValue: { type: Boolean, default: false }, // v-model 控制显示
  title: { type: String, default: '弹出面板' },
  width: { type: [String, Number], default: 480 },
  height: { type: [String, Number], default: 'auto' },
  maskClosable: { type: Boolean, default: true },
  zIndex: { type: Number, default: 3000 }
})
const emit = defineEmits(['update:modelValue', 'close'])

const visible = ref(props.modelValue)
watch(() => props.modelValue, v => visible.value = v)

function close() {
  visible.value = false
  emit('update:modelValue', false)
  emit('close')
}
function handleMaskClick() {
  if (props.maskClosable) close()
}

const panelStyle = computed(() => ({
  width: typeof props.width === 'number' ? props.width + 'px' : props.width,
  height: typeof props.height === 'number' ? props.height + 'px' : props.height,
  zIndex: props.zIndex + 1
}))
</script>

<style scoped>
.popup-panel-mask {
  position: fixed;
  left: 0; top: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.25);
  z-index: 3000;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: popupMaskIn 0.18s;
}
@keyframes popupMaskIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}
.popup-panel-content {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 8px 48px #1e90ff33, 0 2px 8px #fff1 inset;
  min-width: 260px;
  min-height: 120px;
  max-width: 96vw;
  max-height: 96vh;
  display: flex;
  flex-direction: column;
  animation: popupIn 0.22s cubic-bezier(.4,2,.6,1);
  position: relative;
}
@keyframes popupIn {
  from { transform: scale(0.95); opacity: 0.5; }
  to   { transform: scale(1); opacity: 1; }
}
.popup-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 18px;
  font-weight: bold;
  color: #409eff;
  padding: 16px 20px 8px 20px;
  border-bottom: 1px solid #f0f0f0;
}
.popup-panel-title {
  flex: 1;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.popup-panel-close {
  font-size: 22px;
  cursor: pointer;
  color: #bfc6d1;
  transition: color 0.2s;
  margin-left: 12px;
}
.popup-panel-close:hover {
  color: #409eff;
}
.popup-panel-body {
  padding: 20px;
  overflow: auto;
  flex: 1;
}
</style> 